//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2009-01-01
// Contains ...
using System;
using System.Diagnostics.Contracts;
using System.Text;
using System.Xml.Linq;
using JetBrains.Annotations;
using LargoCommon.Abstract;
namespace LargoCommon.Music
{
///
/// Musical System.
///
public sealed class MusicalSystem {
#region Fields
///
/// Harmonic order.
///
private byte harmonicOrder;
///
/// Rhythmic order.
///
private byte rhythmicOrder;
/// Harmonic system.
private HarmonicSystem harmonicSystem;
/// Rhythmical system.
private RhythmicSystem rhythmicSystem;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The system element.
public MusicalSystem(XElement xsystem) {
Contract.Requires(xsystem != null);
if (xsystem == null)
{
return;
}
this.HarmonicOrder = XmlSupport.ReadByteAttribute(xsystem.Attribute("HarmonicOrder"));
this.RhythmicOrder = XmlSupport.ReadByteAttribute(xsystem.Attribute("RhythmicOrder"));
}
///
/// Initializes a new instance of the class.
///
public MusicalSystem()
{
}
#endregion
#region Properties - Xml
///
/// Gets the get x element.
///
///
/// The get x element.
///
public XElement GetXElement {
get {
XElement xsystem = new XElement("System", null);
xsystem.Add(new XAttribute("HarmonicOrder", this.HarmonicOrder));
xsystem.Add(new XAttribute("RhythmicOrder", this.RhythmicOrder));
return xsystem;
}
}
#endregion
#region Properties
///
/// Gets or sets harmonic order.
///
/// Property description.
public byte HarmonicOrder {
get {
Contract.Ensures(Contract.Result() != 0);
if (this.harmonicOrder == 0) {
throw new InvalidOperationException("Harmonic order of system must not be 0.");
}
return this.harmonicOrder;
}
set => this.harmonicOrder = value;
}
///
/// Gets or sets rhythmic order.
///
/// Property description.
public byte RhythmicOrder {
get {
Contract.Ensures(Contract.Result() != 0);
if (this.rhythmicOrder == 0) {
throw new InvalidOperationException("Rhythmic order of system must not be 0.");
}
return this.rhythmicOrder;
}
set => this.rhythmicOrder = value;
}
///
/// Gets the order value.
///
/// Property description.
[UsedImplicitly]
public string OrderValue => MusicalProperties.GetOrderValue(this.HarmonicOrder, this.RhythmicOrder);
/// Gets harmonic system.
/// Property description.
public HarmonicSystem HarmonicSystem {
get {
Contract.Ensures(Contract.Result() != null);
if (this.harmonicSystem != null && this.harmonicSystem.Order == this.HarmonicOrder) {
return this.harmonicSystem;
}
if (this.harmonicSystem == null) {
this.harmonicSystem = HarmonicSystem.GetHarmonicSystem(this.HarmonicOrder);
}
if (this.harmonicSystem == null) {
throw new InvalidOperationException("Harmonic system is null.");
}
this.harmonicSystem = HarmonicSystem.GetHarmonicSystem(this.HarmonicOrder);
return this.harmonicSystem;
}
}
/// Gets rhythmical system.
/// Property description.
public RhythmicSystem RhythmicSystem {
get {
Contract.Ensures(Contract.Result() != null);
if (this.rhythmicSystem != null && this.rhythmicSystem.Order == this.RhythmicOrder) {
return this.rhythmicSystem;
}
if (this.rhythmicSystem == null) {
this.rhythmicSystem = RhythmicSystem.GetRhythmicSystem(RhythmicDegree.Structure, this.RhythmicOrder);
}
if (this.rhythmicSystem == null) {
throw new InvalidOperationException("Rhythmical system is null.");
}
this.rhythmicSystem = RhythmicSystem.GetRhythmicSystem(RhythmicDegree.Structure, this.RhythmicOrder);
return this.rhythmicSystem;
}
}
#endregion
#region String representation
/// String representation of the object.
/// Returns value.
public override string ToString()
{
var s = new StringBuilder();
s.AppendFormat("System H{0} R{1}", this.HarmonicOrder, this.RhythmicOrder);
return s.ToString();
}
#endregion
}
}